﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.database;

namespace ERP_RORZE_PRODUCT.Product.MO
{
    public partial class ctrlMONGCheckInput : DevExpress.XtraEditors.XtraUserControl
    {
        ClsMODAO clsMODAO = new ClsMODAO();
        DataTable dt;
        public ctrlMONGCheckInput()
        {
            InitializeComponent();
        }

        private void ctrlMONGCheckInput_Load(object sender, EventArgs e)
        {
            dt = clsMODAO.GetAllMO();
            List<ClsMOTable> objs = new List<ClsMOTable>();
            foreach (DataRow r in dt.Rows)
            {
                ClsMOTable obj = new ClsMOTable();
                obj.MONo = r["MONo"].ToString();  //
                obj.MOQty = int.Parse(r["MOQty"].ToString());////
                obj.Finished = (bool)r["Finished"];  ////
                objs.Add(obj);
            }
            cbMONo.DataSource = objs;
            cbMONo.DisplayMember = "MONo";
            cbMONo.ValueMember = "MONo";

            //GetAll StaffID
            dt = clsMODAO.GetAllStaffID();
            cbStaffID.DataSource = dt;
            cbStaffID.DisplayMember = "StaffID";
            cbStaffID.ValueMember = "StaffID";

        }

        private void cbMONo_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            ClsMOTable obj = ((ClsMOTable)cbMONo.SelectedItem);
            checkFinish.Checked = obj.Finished;
            txtMOQty.Text = obj.MOQty.ToString();

            ClsMOOptionTable c = new ClsMOOptionTable();
            c.MONo = cbMONo.Text;
            //GetAll OptionID
            dt = clsMODAO.GetAllOptionID(c);
            cbOptionID.DataSource = dt;
            cbOptionID.DisplayMember = "OptionID";
            cbOptionID.ValueMember = "OptionID";
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (checkFinish.Checked == true)
            {
                MessageBox.Show("MO đã hoàn thành. Hãy xem lại");
            }
            else if (cbJustMent.Text == "")
            {
                MessageBox.Show("Chưa chọn JustMent");
            }
            else if(txtQuantity.Text =="")
            {
                MessageBox.Show("Chưa nhập Quantity");
            }
            else if (txtQuantity.Text != "")
            {
                try
                {
                    int.Parse(txtQuantity.Text);
                    if (int.Parse(txtQuantity.Text) > int.Parse(txtMOQty.Text))
                    {
                        MessageBox.Show("Số lượng không được lớn hơn số lượng lệnh");
                    }
                    else
                    {
                        ClsNGCheckTable b = new ClsNGCheckTable();
                        b.MONo = cbMONo.Text;
                        b.Date = dateDate.Value;
                        b.StaffID = cbStaffID.Text;
                        b.OptionID = cbOptionID.Text;
                        b.Justment = cbJustMent.Text;
                        b.Note = txtNote.Text;
                        b.Qty = int.Parse(txtQuantity.Text);

                        clsMODAO.InsertNGCheck(b);

                    }
                }
                catch
                {
                    MessageBox.Show("Quantity phải là số nguyên");
                }
                    

            }
        }
    }
}
